<% report_link = spree.new_admin_report_path(type: 'products_performance', date_from: params[:date_from], date_to: params[:date_to], vendor_id: @vendor.try(:id)) %>

<div class="card mt-3 mb-4">
  <div class="card-header d-flex justify-content-between align-items-center">
    <h5 class="card-title">
      <%= Spree.t('admin.dashboard.top_products') %>
    </h5>
    <% if can?(:manage, Spree::Report) %>
      <%= link_to Spree.t('admin.dashboard.view_report'), report_link, class: 'btn btn-light btn-sm', data: { turbo_frame: '_top' } %>
    <% end %>
  </div>
  <div class="card-body m-0 p-0">
    <div class="table-responsive">
      <table class="table">
        <thead>
          <tr class="border-bottom">
            <th scope="col">
              <%= Spree.t(:product) %>
            </th>
            <% if defined?(Spree::Vendor) && !@vendor %>
              <th scope="col">
                <%= Spree.t(:vendor) %>
              </th>
            <% end %>
            <th scope="col">
              <%= Spree.t(:price) %>
            </th>
            <th class="text-center">
              <%= Spree.t(:quantity) %>
            </th>
            <th class="text-right">
              <%= Spree.t(:total_sales) %>
            </th>
          </tr>
        </thead>
        <tbody>
        <% if @top_products.any? %>
          <% @top_products.each do |list_item| %>
            <% product = list_item[:product] %>
            <tr>
              <% cache_unless @vendor, spree_base_cache_scope.call(product) do %>
                <td>
                  <%= link_to spree.edit_admin_product_path(product), class: 'd-flex align-items-center font-weight-bold', data: { 'turbo-frame': '_top' } do %>
                    <div class="mr-3">
                      <%= render 'spree/admin/shared/product_image', object: product %>
                    </div>
                    <%= product.name %>
                  <% end %>
                </td>
                <% if defined?(vendor_logo_link) && !@vendor %>
                  <td>
                    <%= vendor_logo_link(product.vendor) if product.vendor.present? %>
                  </td>
                <% end %>
                <td>
                  <%= display_admin_price(product) %>
                </td>
              <% end %>
              <td class="text-center">
                <%= list_item[:quantity] %>
              </td>
              <td class="text-right">
                <%= list_item[:amount] %>
              </td>
            </tr>
          <% end %>
        <% else %>
          <tr>
            <td colspan="5" class="text-center text-muted py-5">
              <%= Spree.t(:no_resource_found, resource: Spree::Product.name.demodulize.underscore.humanize) %>
            </td>
          </tr>
        <% end %>
      </tbody>
    </table>
  </div>
  <% if @top_products.any? && can?(:manage, Spree::Report) %>
    <p class="text-center mt-0 border-top py-2 mb-0">
      <%= link_to Spree.t('admin.dashboard.view_report'), report_link, class: 'm-2 w-50 btn btn-light mt-2', data: { turbo_frame: '_top' } %>
    </p>
  <% end %>
  </div>
</div>
